public with sharing class VisualForceController {
	public String test {get;set;}
	public List<String> tList {get;set;}
    public Blob  body ;
    public Integer flag ;//用来标记
   
    public String productJson{get;set;}

    public String sealflag {get;set;}//封存标记

    public String resJson {get;set;}

    public User u;
    //public VisualForceController(ApexPages.StandardController stdController) {
    public VisualForceController() {
        
        //resJson = '';
        u = [select Id,SubCompany__c from User where Id=:UserInfo.getUserId()];
        body = null;
         ///获取封存标记
        Map<String,String> cm = getPicklistValues('Contract__c','Corpcode__c');
        String concode = cm.get(u.SubCompany__c);
        sealflag = 'Y-'+concode;
    }




    //获得PickList的值
    public  Map<String, String> getPicklistValues(String sObjectName, String sFieldName){
        Map<String, String> picValues= new Map<String, String>();
        Map<String, Schema.SObjectType> sObjectDescribeMap = Schema.getGlobalDescribe();
        if (sObjectDescribeMap.containsKey(sObjectName)) {
            Map<String, Schema.SobjectField> sObjectFieldsMap = 
               sObjectDescribeMap.get(sObjectName).getDescribe().fields.getMap();
            if (sObjectFieldsMap.containsKey(sFieldName)) {
                Schema.DescribeFieldResult sObjectFieldDescribe = sObjectFieldsMap.get(sFieldName).getDescribe();
                List<Schema.PicklistEntry> lPickEntryList = sObjectFieldDescribe.getPicklistValues();
                for (Schema.PicklistEntry tPicklistEntry : lPickEntryList) {
                    if(tPicklistEntry.isActive() == true){
                        picValues.put(tPicklistEntry.getLabel(),tPicklistEntry.getValue());
                    }
                    /*picValues.put(tPicklistEntry.getValue(), new Map<String,object>{
                        'value' => tPicklistEntry.getValue(),
                        'isActive' => tPicklistEntry.isActive(),
                        'isDefaultValue' => tPicklistEntry.isDefaultValue(),
                        'label' => tPicklistEntry.getLabel()
                    });*/
                }
            } else {
                //TODO 不存在此字段的执行
            }
        } else {
            // TODO 不存在此sObject的执行
        }
        return picValues;
    }

    public String test(){

        return '1234567890';
    }

    public void  getProduct(){


        //System.debug(System.currentPageReference().getParameters('data'));

        //List<Product__c> plist = Database.query(this.soqlStr() + whereStr + ' limit 300');
       List<Object> os = (List<Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(resJson,'UTF-8')) ;

        Set<String> prodcuts = new Set<String>();
        for ( Object o : os) {
               Map<String ,Object> res = (Map<String ,Object> )o ;
               prodcuts.add('nccode');

         }  
         List<Product__c> plist = [select Id ,Name,NCCode__c from Product__c where NCCode__c in:prodcuts];

         List<ProductBean> resList = new List<ProductBean>();
        // 获取价格信息
        Map<Id, Price__c> priceMap = new Map<Id, Price__c>();
        List<Id> pidList = new List<Id>();
        
        for (Product__c p : plist) {
            if(!(p.SealCompany__c != null && p.SealCompany__c.contains(sealflag))){
                pidList.add(p.Id);
            }else{
                resList.add(new ProductBean(p,'产品被封存'));
            }
            if(!prodcuts.contains(p.NCCode__c))resList.add(new ProductBean(p,'没有找到产品,请确认存货编码'));
        }
        for (Price__c pr : [select Id, Product__c, MarketPrice__c, AreaDealerPrice__c, InvoiceDealerPrice__c from Price__c where Product__c in : pidList and Company__c = :u.SubCompany__c]) {
            priceMap.put(pr.Product__c, pr);
        }

         List<ProductBean> pbList = new List<ProductBean>();
        for (Product__c p : plist) {
            if(priceMap.containsKey(p.Id)){
                pbList.add(new ProductBean(p));
                //Price__c pr = priceMap.get(p.Id);
            }else{
                resList.add(new ProductBean(p,'产品没有价格体系'));
            }
            
        }
        Map<String,List<ProductBean>> res = new Map<String,List<ProductBean>>();
        res.put('data', pbList);
        res.put('rows', resList);
            productJson=EncodingUtil.urlEncode(JSON.serialize(res), 'UTF-8');

            
    }

    public class ProductBean{

        public String productId {get;set;}//产品Id

        public String productName {get;set;}//产品名称

        public String nccode {get;set;}//存货编码\

        public String mark {get;set;}

        public ProductBean(Product__c p){

            productId = p.Id;
            productName = p.Name;
            nccode = p.NCCode__c;

        }

        public ProductBean(Product__c p,String m){

            productId = p.Id;
            productName = p.Name;
            nccode = p.NCCode__c;
            mark = m;
            
        }
    }
    public void init(){


        /* String s = System.currentPageReference().getParameters().get('fileValue');

        System.debug(System.currentPageReference().getParameters());

         PageReference  currentPage = System.currentPageReference();
            Map<String, String> params  =  currentPage.getParameters(); 
                //remove ekp and save_new key
                params.remove('fileValue');
                currentPage.getParameters().putAll(params);
                
        System.debug(System.currentPageReference().getParameters());




        if(s != null && s.length() >0){
            System.debug(s.substring(s.indexOf(',')) );
            body = EncodingUtil.base64Decode(s.substring(s.indexOf(',')+1) );
            System.debug(body);
           

           
        }
        if(s == null) body = null;
       if(s != null ) System.debug(s.length());
        System.debug(test);
        if(body != null){
           Attachment att = new Attachment();
            att.Body = body;
            att.ParentId = 'a1F28000000hPQ0';
            att.Name = System.currentPageReference().getParameters().get('upfile');

           insert att; 
        }
        body = null;*/
         
    }

    public void save(){
    	System.debug('+++'+test);
    }
    public void submit(){
        System.debug(System.currentPageReference().getParameters());

    }


    // Required extension constructor (empty, no-op)
    public VisualForceController(ApexPages.StandardController controller) {}
    
    // Determines what kind of rendering to use for the page request
    public String renderingService { get; private set; }
    
    // Allow the page to set the PDF file name
    public String renderedFileName { 
        get; 
        set { renderedFileName = this.sanitizeFileName(value); }
    }

    // Rendered content MIME type, used to affect HTTP response
    public String renderedContentType {
        get {
            String renderedContentType = 'text/html'; // the default
            
            if( ! this.renderingAsHtml() ) {
                // Provides a MIME type for a PDF document 
                renderedContentType = 'application/pdf';
                
                // Add a file name for the PDF file
                if( this.renderedFileName != null) {
                    // This is supposed to set the file name, but it doesn't work
                    renderedContentType += '#' + this.renderedFileName;
                    
                    // This is a work-around to set the file name
                    ApexPages.currentPage().getHeaders().put(
                        'content-disposition', 'attachment; filename=' + 
                         this.renderedFileName);
                }
            }
            
            return renderedContentType;
        }
    }
    
    // Are we rendering to HTML or PDF?
    public Boolean renderingAsHtml() {
        return ( (renderingService == null) || 
                 ( ! renderingService.startsWith('PDF')) );
    }

    // Action method to save (or "print") to PDF
    public PageReference saveToPdf() {
        renderingService = 'PDF';
        return null;
    }
    
    // Private helper -- basic, conservative santization
    private String sanitizeFileName(String unsafeName) {
        String allowedCharacters = '0-9a-zA-Z-_.';
        String sanitizedName = 
            unsafeName.replaceAll('[^' + allowedCharacters + ']', '');
        // You might also want to check filename length, 
        // that the filename ends in '.pdf', etc.
        return(sanitizedName);
    }
}